package com.ossjk.oa.system.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ossjk.core.util.CommonMethod;
import com.ossjk.oa.system.entity.Attachment;
import com.ossjk.oa.system.mapper.AttachmentMapper;
import com.ossjk.oa.system.service.IAttachmentService;

/**
 * <p>
 * 附件表 服务实现类
 * </p>
 *
 * @author chair
 * @since 2020-05-24
 */
@Service
public class AttachmentServiceImpl extends ServiceImpl<AttachmentMapper, Attachment> implements IAttachmentService {
	
	@Value("${files.path}")
	private String filesPath;
	
	@Override
	public Page selectVoPage(Page page, Wrapper wrapper) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Attachment uploadOneAttachment(Attachment atta, MultipartFile mfile) {

		File baseDir = new File(filesPath + File.separator + CommonMethod.iso0Format.format(new Date()));
		if (!baseDir.exists()) {
			baseDir.mkdirs();
		}

		// 删除旧文件
		File arraFile = new File(baseDir, atta.getFile() );
		if (arraFile.exists()) {
			arraFile.delete();
		}

		try {
			mfile.transferTo(arraFile);
			atta.setUrl(baseDir.getAbsolutePath());

			this.baseMapper.insert(atta);

			return atta;

		} catch (IllegalStateException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
